using System.Collections.Generic;
using System.Linq;
using Ninject;
using sdm.exam.domain.Abstract;

namespace sdm.exam.domain.DataContext
{
    public class ExamDomain
    {
        readonly ISdmExamRepository _repository = Ninjector.Kernel.Get<ISdmExamRepository>();
      
        /// <summary>
        /// Gets a complete list of exams
        /// </summary>
        /// <returns>List of all exams</returns>
        public List<Entities.Exam> GetListOfExams()
        {
            var q = from item in _repository.Exams select item;
            return q.ToList();
        }

        /// <summary>
        /// Add an new exam
        /// </summary>
        /// <param name="exam">The exam to add</param>
        public void AddExam(Entities.Exam exam)
        {
            _repository.Exams.Add(exam);
            _repository.SaveChanges();
        }

        /// <summary>
        /// Gets a list of exams by specified education.
        /// </summary>
        /// <param name="education">The specified education to check</param>
        /// <returns>A list of exams matching the education</returns>
        public List<Entities.Exam> GetExamsListByEducation(Entities.Education education)
        {
            var edu = from item in _repository.Educations
                      where item.Name == education.Name
                      select item;

            var examsList = _repository.Exams.Where(exams => exams.Education == edu.FirstOrDefault());

            return examsList.ToList();
        }

        /// <summary>
        /// Gets a list of exams for a specific semester
        /// </summary>
        /// <param name="semester">Defines the specific semester</param>
        /// <returns>A list of all exams for a given semester</returns>
        public List<Entities.Exam> GetExamsListForSemester(int semester)
        {
            var examsList = _repository.Exams;

            return (from exam in examsList
                    let calculator = new SemesterCalcManager()
                    let calcSemester = calculator.CalculateSemester(exam.SemesterClass.StartDate)
                    where calcSemester == semester 
                    select exam).ToList();
        }
    }
}
